package com.itheima.controller1;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * 发送短信校验
 * Author chongge
 * Time 2019/10/29 19:41
 * ClassName com.itheima.controller1
 */
@RestController
@RequestMapping(value = "/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;


    /***
     * 预约时所需的验证码
     * @param telephone
     * @return
     */
    @RequestMapping(value = "/send4Order")
    public Result send4Order(String telephone){
        return sendMessage(telephone,RedisMessageConstant.SENDTYPE_ORDER);
    }

    /***
     * 发送验证码
     * @param telephone
     * @param type
     * @return
     */
    private Result sendMessage(String telephone,String type) {
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        try {
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            System.out.println("================验证码为:"+code+"===================");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        jedisPool.getResource().setex(type+"_"+telephone,5*60,code.toString());
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /***
     * 登录发送验证码
     * @param telephone
     * @return
     */
    @RequestMapping(value = "/send4Login",method = RequestMethod.POST)
    public Result send4Login(String telephone){
        return sendMessage(telephone,RedisMessageConstant.SENDTYPE_LOGIN);
    }
}
